﻿using FirstFloor.ModernUI.Presentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;

namespace EasyInvoice.WPF.ViewModel
{
    public class InvoiceDetailViewModel : NotifyPropertyChanged
    {
        private Model.InvoiceDetail _detail = null;

        private Services.InvoiceDetailsService _serviceInvoiceDetail;

        public InvoiceDetailViewModel() :this( new Model.InvoiceDetail()){}

        public InvoiceDetailViewModel(Model.InvoiceDetail detail)
        {
            this._detail = detail;
            this._serviceInvoiceDetail = appCtx.Instance.IoC.Resolve<Services.InvoiceDetailsService>();
        }

        public int Index { get; set; }

        public int Id
        {
            get
            {
                return this._detail.Id;
            }
            set
            {
                this._detail.Id = value;
                this.OnPropertyChanged("Id");
            }
        }

        public string Description
        {
            get
            {
                return this._detail.Description;
            }
            set
            {
                this._detail.Description = value;
                this.OnPropertyChanged("Description");
            }
        }

        public decimal Price
        {
            get
            {
                return this._detail.Price;
            }
            set
            {
                this._detail.Price = value;
                this.OnPropertyChanged("Price");
                this.OnPropertyChanged("Value");
            }
        }

        public int Units
        {
            get
            {
                return this._detail.Units;
            }
            set
            {
                this._detail.Units = value;
                this.OnPropertyChanged("Units");
                this.OnPropertyChanged("Value");
            }
        }

        public decimal Value
        {
            get
            {
                return this._detail.Price * this._detail.Units;
            }
        }

        public int InvoiceId
        {
            get
            {
                return this._detail.InvoiceId;
            }
            set
            {
                this._detail.InvoiceId = value;
                OnPropertyChanged("InvoiceId");

            }
        }

        internal void Save()
        {
            this._serviceInvoiceDetail.Save(this._detail);
        }
    }
}
